package com.jctrip.hotel.boot.config;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import feign.codec.Decoder;

/**
 * feign第三方接口配置类
 */
@Configuration
@EnableFeignClients(basePackages = "com.jctrip.hotel.**.client")
public class FeignConfig {

    @Bean
    public Decoder feignDecoder() {
        return new FeignResponseDecoder(new SpringDecoder(feignHttpMessageConverter()));
    }

    public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        // 先将原先支持的MediaType列表拷出
        List<MediaType> mediaTypeList = new ArrayList<>(jsonConverter.getSupportedMediaTypes());
        //加入对text/plain的支持
        mediaTypeList.add(new MediaType("text", "json", StandardCharsets.UTF_8));
        // 加入对 text/html 的支持
        mediaTypeList.add(MediaType.TEXT_HTML);
        //将已经加入了text/json的MediaType支持列表设置为其支持的媒体类型列表
        jsonConverter.setSupportedMediaTypes(mediaTypeList);
        HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jsonConverter);
        return () -> httpMessageConverters;
    }
}

